package com.jeeplus.modules.api;


import com.jeeplus.modules.brand.entity.Brand;
import com.jeeplus.modules.brand.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("/api/")
@Slf4j
public class JsoupController {

    private final BrandService brandService;

    public JsoupController(BrandService brandService) {
        this.brandService = brandService;
    }

    @RequestMapping("jsoup")
    public void jsoup() throws IOException {
        String url = "http://product.auto.163.com/#DQ2001";
        Connection.Response response = org.jsoup.Jsoup.connect(url).validateTLSCertificates(false).ignoreContentType(true).ignoreHttpErrors(true).execute();
        Document document = response.parse();
        Elements select1 = document.select("div.brand_name");
        int i = 1;
        for (Element element : select1) {
            // 创建对象
            Brand brand = new Brand();
            Elements div = element.getElementsByTag("div");
            String letter = div.attr("data-letter");
            Element h2 = element.selectFirst("h2");
            String title = h2.attr("title");
            brand.setBrand(title);
            brand.setBrandType("1");
            brand.setLetter(letter);
            brand.setParentIds("0,");
            Brand brand1 = new Brand();
            brand1.setIsNewRecord(true);
            brand1.setId("0");
            brand.setSort(i++);
            brand.setParent(brand1);
            //  获取li标签
            Elements li = element.getElementsByClass("chexi-list");
            Elements li2 = element.select("a");
            Element element4 = li2.get(0);
            String href1 = element4.attr("href");
            String uri = "http://product.auto.163.com" + href1;
            Connection.Response response9 = org.jsoup.Jsoup.connect(uri).validateTLSCertificates(false).ignoreContentType(true).ignoreHttpErrors(true).execute();
            Document document9 = response9.parse();
            Elements select9 = document9.select("div.img-text");
            Elements div1 = select9.select("img");
            String src = div1.attr("src");
            brand.setIcon(src);
            brandService.save(brand);
            for (Element element1 : li) {
                Elements li1 = element1.getElementsByTag("a");
                for (Element element2 : li1) {
                    String href = element2.attr("href");
                    String title1 = element2.attr("title");
                    // 添加车型
                    Brand brand2 = new Brand();
                    brand2.setBrand(title);
                    brand2.setParent(brand);
                    brand2.setLetter(letter);
                    brand2.setBrandType("2");
                    brand2.setParentIds(brand.getParentIds() + brand.getParentIds() + ",");
                    brand2.setMotorcycleType(title1);
                    brandService.save(brand2);
                    // 获取车型信息
                    Connection.Response response1 = org.jsoup.Jsoup.connect(href).validateTLSCertificates(false).ignoreContentType(true).ignoreHttpErrors(true).execute();
                    Document document1 = response1.parse();
                    // 获取车型数据
                    Elements select = document1.select("div.row_data");
                    for (Element element3 : select) {
                        Element a = element3.getElementsByTag("a").first();
                        String text = a.text();
                        // 添加车款
                        Brand brand3 = new Brand();
                        brand3.setBrand(title);
                        brand3.setParent(brand2);
                        brand3.setBrandType("3");
                        brand3.setLetter(letter);
                        brand3.setParentIds(brand2.getParentIds() + brand2.getParentIds() + ",");
                        brand3.setMotorcycleType(title1);
                        brand3.setCarStyle(text);
                        brand3.setYearStyle(Integer.parseInt(text.substring(0, 4)));
                        brandService.save(brand3);
                    }
                }
            }
            System.out.println("~~~~~~~~~~~~~~~~~~");
        }
    }
}
